﻿using System.Web;
using DoNet.Helper;

namespace DoNet.Platform.RenRen
{
    /// <summary>
    /// 人人网 Access Token
    /// </summary>
    public class TokenApi
    {
        /// <summary>
        /// 获取Access Token
        /// </summary>
        /// <param name="client_id"></param>
        /// <param name="client_secret"></param>
        /// <param name="redirect_uri"></param>
        /// <param name="code">用户授权后得到code </param>
        /// <returns></returns>
        public static ReturnData GetToken(string client_id, string client_secret, string redirect_uri, string code)
        {
            /*
            baseUrl + "/token?grant_type=authorization_code" + "&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s";
            */
            string reqUrl = CommonConfig.WebApiUrl_RenRen_graph + string.Format("/oauth/token?grant_type=authorization_code&client_id={0}&client_secret={1}&code={2}&redirect_uri={3}", client_id, client_secret, code, redirect_uri);
            return HttpHelper.HttpGet(reqUrl);
        }

        /// <summary>
        /// 获取授权URL
        /// </summary>
        /// <param name="app_key">应用标识</param>
        /// <param name="redirect_uri">回调地址,必须与开发者控制里的URL保持一致</param>
        /// <param name="scope">应用的作用域 snsapi_base（静默模式，登录+授权一步完成） snsapi_union_login（拉端模式，拉起京东APP端完成授权） snsapi_app_union_login（非拉端模式，当前APP直接打开H5授权页完成授权） scope=snsapi_jdjr_union_login （（拉端模式，拉起京东金融APP端完成授权））</param>
        /// <param name="state">维持应用的状态，传入值与返回值保持一致。</param>
        /// <returns></returns>
        public static string GetAuthorizeUrl(string app_key, string redirect_uri, string scope, string state = "")
        {
            /*
            return "https://graph.renren.com/oauth/authorize?response_type=code&client_id={0}&redirect_uri={1}&state={2}";
            */
            string param = string.Format("/oauth/authorize?response_type=code&client_id={0}&redirect_uri={1}&state={2}", app_key, HttpUtility.UrlEncode(redirect_uri), state);
            if(!string.IsNullOrWhiteSpace(state))
            {
                param += string.Format("&state={0}", state);
            }
            return CommonConfig.WebApiUrl_RenRen_graph + param;
        }
    }
}
